package com.lzx.iteam.provider;

import android.content.*;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import com.lzx.iteam.provider.CloudContactsDB.CloudGroupDbo;
import com.lzx.iteam.provider.CloudContactsDB.CommunicationRule;

/**
 * Created by hfx on 14-12-23.
 */
public class CloudContactsProvider extends ContentProvider {
    //数库存操作帮助类
    private SQLiteOpenHelper mHelper;
    //URI匹配器
    private static UriMatcher mUriMatcher;
    //云号码群匹配码
    private static final int CLOUD_GROUP = 1;
    //黑名单匹配码
    private static final int CommunicationRule_DATA = 2;

    //同步锁
    private Object mLock = new Object();

    //初始化匹配规则
    static {
        mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//        mUriMatcher.addURI(CloudContactsDB.AUTHORITY, CloudGroupDbo.PATH,CLOUD_GROUP);
//        mUriMatcher.addURI(CloudContactsDB.AUTHORITY, CommunicationRule.PATH,CommunicationRule_DATA);
    }

    @Override
    public boolean onCreate() {
        getInstance(getContext());
        return true;
    }

    public synchronized  SQLiteOpenHelper getInstance(Context context) {
        if (mHelper == null) {
            mHelper = new CloudContactsDBHelper(context);
        }
        return mHelper;
    };

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor cursor = null;
        SQLiteQueryBuilder sqb = new SQLiteQueryBuilder();
        SQLiteDatabase db = mHelper.getReadableDatabase();
        StringBuilder sb = new StringBuilder();
        int match = mUriMatcher.match(uri);
        switch (match){
            case CLOUD_GROUP:
                sqb.setTables(CloudGroupDbo.TABLE_NAME);
//                sqb.setProjectionMap(CloudGroupDbo.CLOUD_GROUP_MAP);
                cursor = sqb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case CommunicationRule_DATA:
                sqb.setTables(CommunicationRule.TABLE_NAME);
                sqb.setProjectionMap(CommunicationRule.sProjectionMap);
                cursor = sqb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
                break;
            default:
                throw new IllegalArgumentException("Unknown URL " + uri);
        }
        if (cursor != null) {
            cursor.setNotificationUri(getContext().getContentResolver(), uri);
        }
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        String type = null;
        int match = mUriMatcher.match(uri);
        switch (match){
            case CLOUD_GROUP:
//                type = CloudGroupDbo.CONTENT_TYPE;
                break;
            case CommunicationRule_DATA:
//                type = CommunicationRule.CONTENT_TYPE;
                break;
             default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        return type;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        int match = mUriMatcher.match(uri);
        long id = -1;
        synchronized (mLock){
            db.beginTransaction();
            try{
                switch (match){
                    case CLOUD_GROUP:
                        id = db.insert(CloudGroupDbo.TABLE_NAME, "", values);
                        db.setTransactionSuccessful();
                        break;
                    case CommunicationRule_DATA:
                        id = db.insert(CommunicationRule.TABLE_NAME,"",values);
                        db.setTransactionSuccessful();
                        break;
                    default:
                        throw new IllegalArgumentException("Unknown URL " + uri);
                }
            }finally {
                db.endTransaction();
            }
        }
        Uri result = null;
        if (id > 0) {
            result =  ContentUris.withAppendedId(uri, id);
        }
        return result;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int count = 0;
        SQLiteDatabase db = mHelper.getWritableDatabase();
        StringBuilder sb = new StringBuilder();
        int match = mUriMatcher.match(uri);
        db.beginTransaction();
        try {
            switch (match){
                case CLOUD_GROUP:
                    count = db.delete(CloudGroupDbo.TABLE_NAME, selection, selectionArgs);
                    db.setTransactionSuccessful();
                    break;
                case CommunicationRule_DATA:
                    count = db.delete(CommunicationRule.TABLE_NAME,selection,selectionArgs);
                    db.setTransactionSuccessful();
                    break;

                default:
                    throw new IllegalArgumentException("Cannot delete that URL: " + uri);
            }
        }finally {
            db.endTransaction();
        }
        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int count = 0;
        SQLiteDatabase db = mHelper.getWritableDatabase();
        StringBuilder sb = new StringBuilder();
        int match = mUriMatcher.match(uri);
        db.beginTransaction();
        try {
            switch (match){
                case CLOUD_GROUP:
                    count = db.update(CloudGroupDbo.TABLE_NAME, values, selection, selectionArgs);
                    db.setTransactionSuccessful();
                    break;
                case CommunicationRule_DATA:
                    count = db.update(CommunicationRule.TABLE_NAME,values,selection,selectionArgs);
                    db.setTransactionSuccessful();
                    break;
                default:
                    throw new UnsupportedOperationException("Cannot update URL: " + uri);
            }
        }finally {
            db.endTransaction();
        }
        return count;
    }
}
